<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试名称" prop="recordName" class="item">
        <el-input
          v-model="queryParams.recordName"
          placeholder="请输入考试名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="考试类型" prop="recordType" class="item">
        <el-select v-model="queryParams.recordType" placeholder="请选择考试类型" clearable size="small">
          <el-option
            v-for="dict in recordTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      
      
      <el-form-item label="所属科室" prop="dept" class="item">
          <treeselect v-model="queryParams.deptId" clearable :options="deptOptions" 
          @keyup.enter.native="handleQuery" style="width:200px;"
          :disable-branch-nodes="false" :show-count="true" placeholder="请选择所属科室" />
      </el-form-item>

      <el-form-item label="用户名称" prop="nickName" class="item">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称（全名）"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="考试时间" class="item">
          <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ability:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ability:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ability:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ability:record:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="recordId" v-if="false"/>
      <el-table-column label="考试名称" align="center" prop="recordName" />
      <el-table-column label="考试类型" align="center" prop="recordType" :formatter="recordTypeFormat" />
      <el-table-column label="所属科室" align="center" prop="deptName" />
      <el-table-column label="考试时间" align="center" prop="examTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> 
      <el-table-column label="平均分数" align="center" prop="avgScore" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['ability:record:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetails(scope.row)" v-hasPermi="['ability:details:list']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['ability:record:remove']" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考试名称" prop="recordName">
          <el-input v-model="form.recordName" placeholder="请输入考试名称" style="width:350px" />
        </el-form-item>
        <el-form-item label="考试类型">
          <el-select v-model="form.recordType" placeholder="请选择考试类型" style="width:350px">
            <el-option
              v-for="dict in recordTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所属科室" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属科室"  style="width:350px"/>
        </el-form-item> -->
        <el-form-item label="所属科室" prop="deptId" >
          <treeselect :options="deptOptions"  placeholder="请输入所属科室"
          :disable-branch-nodes="true" v-model="form.deptId" search-nested style="width:350px"/>
        </el-form-item>
        <el-form-item label="考试时间" prop="examTime">
          <el-date-picker clearable size="small" style="width:350px"
            v-model="form.examTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择考试时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord, listDetails } from "@/api/ability/people/record";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Record",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 部门树选项
      deptOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 考试类型字典
      recordTypeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordName: null,
        nickName:null,
        recordType: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
         recordName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" }
        ],
        recordType: [
          { required: true, message: "考试类型不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属科室不能为空", trigger: "blur" }
        ],
        examTime: [
          { required: true, message: "考试时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("ability_exam_type").then(response => {
      this.recordTypeOptions = response.data;
    });
  },

  methods: {
    /** 查询考试记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {   
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 考试类型字典翻译
    recordTypeFormat(row, column) {
      return this.selectDictLabel(this.recordTypeOptions, row.recordType);
    },
     /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        recordName: null,
        nickName: null,
        recordType: null,
        deptId: null,
        examTime: null,
        avgScore: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加考试记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const recordId = row.recordId || this.ids
      getRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateRecord(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRecord(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$confirm('是否确认删除考试记录编号为"' + recordIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(recordIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考试记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      this.$router.push({
        path:'/ability/people/details',
        query:{
          id:row.recordId,
          name:row.recordName,
          data:row,
        }
      })

    },
  }
};
</script>
